/* $RCSFile: ADNDynSalesLiterature.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/CRMWrapper3/DynEntities/ADNDynSalesLiterature.cs $
 * $log$
 * Revision 19 2010/10/02 14:01:49  christian.surieux
 *   Corrected a bug in allmost everyADNDyn file for PrimaryFieldVualue
 * Revision 6 2010/08/06 19:56:33  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Diagnostics;
using System.Collections;
using System.ComponentModel;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

using CrmSdk;

namespace AddonNice.CRMWrapper
{
	public class ADNDynSalesLiterature : DynEntity
	{
        protected class Ftor: DFactor
        {
            public Ftor() : base(CrmStr.salesliterature)
            {}

            public override Type DynType
            { get { return typeof(ADNDynSalesLiterature); } }

            public override string DynTypename
            {   get{ return CrmStr.salesliterature;}}

            public override DynEntity Build()
            {   return new ADNDynSalesLiterature();}

            public override DynEntity Build(CRMWrapperBase w, InternalDynamicEntity dEntity, PropertyDescriptorCollection wproperties)
            {   return new ADNDynSalesLiterature(w, dEntity, wproperties, null);}

            public override DynEntity Build(CRMWrapperBase w, string PrimaryFieldValue)
            {   return new ADNDynSalesLiterature(w,PrimaryFieldValue);}

            public override DynEntity Build(CRMWrapperBase w,Guid PrimaryKeyValue)
            {   return new ADNDynSalesLiterature(w,PrimaryKeyValue);}
        }

        static ADNDynSalesLiterature(){new Ftor();} 

        public override bool HasOwnerProperty
        {
            get
            {
                return false;
            }
        }

        public override bool HasBusinessUnitProperty
        {
            get
            {
                return false;
            }
        }

        public ADNDynSalesLiterature(): base()
		{}

        public ADNDynSalesLiterature(CRMWrapperBase w, string PrimaryFiedlValue)
			: base(w, CrmStr.salesliterature)
		{
            if ( PrimaryFiedlValue != string.Empty )
                name    =   PrimaryFiedlValue;
		}

		public ADNDynSalesLiterature(CRMWrapperBase w, Guid CrmId)
			: base(w,CrmStr.salesliterature, CrmId)
		{}

		public ADNDynSalesLiterature(CRMWrapperBase w, InternalDynamicEntity e)
			: base(w,CrmStr.salesliterature,e,null,null)
		{}

		public ADNDynSalesLiterature(CRMWrapperBase w, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(w,CrmStr.salesliterature,e,prop,s)
		{}

        public ADNDynSalesLiterature(ResetWrapperHandler fn, string PrimaryFiedlValue)
			: base(fn, CrmStr.salesliterature)
		{
            if ( PrimaryFiedlValue != string.Empty )
                name    =   PrimaryFiedlValue;
		}

		public ADNDynSalesLiterature(ResetWrapperHandler fn, Guid CrmId)
			: base(fn,CrmStr.salesliterature, CrmId)
		{}

		public ADNDynSalesLiterature(ResetWrapperHandler fn, InternalDynamicEntity e)
			: base(fn,CrmStr.salesliterature,e,null,null)
		{}

		public ADNDynSalesLiterature(ResetWrapperHandler fn, InternalDynamicEntity e,PropertyDescriptorCollection prop,InternalColumnSet s)
			: base(fn,CrmStr.salesliterature,e,prop,s)
		{}
		
        /// <summary>
		/// Crm Key
		/// </summary>
        [CrmDynAttribute(CrmStr.salesliterature)]
        public Guid salesliteratureid
		{
			get
			{
				return KeyPropertyValue(CrmStr.salesliteratureid);
			}
			set
			{
				AddKeyProperty(CrmStr.salesliteratureid, value);
			}
		}

        protected override void SetCrmIdProperty(Guid crmId)
        {
           if ( !dicProperty.ContainsKey(CrmStr.salesliteratureid) || KeyPropertyValue(CrmStr.salesliteratureid) != crmId )
                salesliteratureid   =   crmId;
        }
        
        public override string PrimaryFieldName
        {
            get
            {
               return CrmStr.name;
            }
           set
           {}
        }

        /// <summary>
        /// Value contained in the PrimaryField for the DynEntity or empty if no PrimaryField
        /// </summary>
        public override string PrimaryFieldStringValue
        {
            get
            {
                return name;
            }
            set
            {
                name =   value;
            }
        }

        public override Guid PrimaryKeyGuidValue
        {
           get
           {
                return salesliteratureid;
           }
           set
           {
               salesliteratureid    =   value;
           }
        }

        [CrmDynAttribute(CrmStr.salesliterature)]
        public override string name
        {
            get
            {
                return StringPropertyValue(CrmStr.name);
            }
            set
            {
                AddStringProperty(CrmStr.name, value);
            }
        }

        [CrmDynAttribute(CrmStr.salesliterature)]
        public string description 
        {
            get
            {
                return StringPropertyValue(CrmStr.description);
            }
            set
            {
                AddStringProperty(CrmStr.description,value);
            }
        }

        /// <summary>
        /// Gets or sets the mimetype of the annotation attached file. 
        /// </summary>
        [CrmDynAttribute(CrmStr.salesliterature)]
        public string keywords 
        {
            get
            {
                return StringPropertyValue(CrmStr.keywords);
            }
            set
            {
                AddStringProperty(CrmStr.keywords,value);
            }
        }
        
        [CrmDynAttribute(CrmStr.salesliterature)]
        public bool hasattachments  
        {
            get
            {
                return BooleanPropertyValue(CrmStr.hasattachments );
            }
            set
            {
                AddBooleanProperty(CrmStr.hasattachments ,value);
            }
        }

         
        [CrmDynAttribute(CrmStr.salesliterature)]
        public bool iscustomerviewable  
        {
            get
            {
                return BooleanPropertyValue(CrmStr.iscustomerviewable);
            }
            set
            {
                AddBooleanProperty(CrmStr.iscustomerviewable,value);
            }
        }

        [CrmDynAttribute(CrmStr.salesliterature)]
        public string expirationdate  
        {
            get
            {
                return DateTimePropertyValue(CrmStr.expirationdate);
            }
            set
            {
                AddDateTimeProperty(CrmStr.expirationdate,value);
            }
        }

        public DateTime expirationdateUTCDate
        {
            get
            {
                return DateTimePropertyDate(CrmStr.expirationdate,false);
            }
        }

        public DateTime expirationdateLocalDate
        {
            get
            {
                return DateTimePropertyDate(CrmStr.expirationdate,true);
            }
        }

        [CrmDynAttribute(CrmStr.salesliterature)]
        public Guid employeecontactid
        {
            get
            {
                if (ContainsKey(CrmStr.employeecontactid))
                    return LookupPropertyValue(CrmStr.employeecontactid);
                return Guid.Empty;
            }
            set
            {
                AddLookupProperty(CrmStr.employeecontactid,CrmStr.systemuser,value);
            }
        }

        [CrmDynAttribute(CrmStr.salesliterature)]
        public string employeecontactName
        {
            get
            {
                if (ContainsKey(CrmStr.employeecontactid))
                    return LookupPropertyName(CrmStr.employeecontactid);
                return string.Empty;
            }
        }
        
        /// <summary>
        /// Gets or sets the ID of the subject associated with the knowledge base article 
        /// </summary>
        [CrmDynAttribute(CrmStr.salesliterature)]
        public Guid subjectid
        {
            get
            {
                if (ContainsKey(CrmStr.subjectid))
                    return LookupPropertyValue(CrmStr.subjectid);
                return Guid.Empty;
            }
            set
            {
                AddLookupProperty(CrmStr.subjectid,CrmStr.subject,value);
            }
        }

        [CrmDynAttribute(CrmStr.salesliterature)]
        public string subjectName
        {
            get
            {
                if (ContainsKey(CrmStr.subjectid))
                    return LookupPropertyName(CrmStr.subjectid);
                return string.Empty;
            }
        }

        /// <summary>
        /// Bulletins
        /// Company Background
        /// Manuals
        /// Marketing Collateral
        /// News
        /// Policies And Procedures
        /// Presentation
        /// Price Sheets
        /// Product Sheet
        /// Sales Literature
        /// Spreadsheets 
        /// Default value1 Policies And Procedures 
        /// </summary>
        [CrmDynAttribute(CrmStr.salesliterature)]
        public int literaturetypecode 
        {
            get
            {
                return PicklistPropertyIntValue(CrmStr.literaturetypecode );
            }
            set
            {
                AddPicklistProperty(CrmStr.literaturetypecode ,value);
            }
        }

        public string literaturetypecodeLabel
        {
            get
            {
                return PicklistPropertyLabelValue(CrmStr.literaturetypecode );
            }
        }

        public void SetRelated(ADNDynProduct prod,bool isAdmin)
        {
            wrapper.Util.SetRelated(this,prod,isAdmin);
        }


         public List<ADNDynProduct> GetRelatedProducts(ResetWrapperHandler fn,bool isAdmin)
        {
            if ( CRMDynTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynProduct GetRelatedProducts BEG name: {0}, isAdmin: {1}",name,isAdmin),CRMDynTraceSwitch.Sw.Info);
            List<ADNDynProduct> List                                =   new List<ADNDynProduct>();
            try
            {
                GenericDynQueryMultiple<ADNDynProduct> allEntQuery  =   new GenericDynQueryMultiple<ADNDynProduct>(wrapper,fn,CrmStr.product);
                InternalQueryExpression qe                          =   new InternalQueryExpression();
                qe.EntityName                                       =   CrmStr.product;
                InternalAllColumns RequestCols                      =   new InternalAllColumns();
                
                // link from product entity to productassociation
                InternalLinkEntity le                               =   new InternalLinkEntity();
                le.LinkFromEntityName                               =   CrmStr.product;
                le.LinkFromAttributeName                            =   CrmStr.productid;
                le.LinkToEntityName                                 =   CrmStr.productsalesliterature;
                le.LinkToAttributeName                              =   CrmStr.productid;

                // link from productassociation to product
                InternalLinkEntity le2                              =   new InternalLinkEntity();
                le2.LinkFromEntityName                              =   CrmStr.productsalesliterature;
                le2.LinkFromAttributeName                           =   CrmStr.salesliteratureid;
                le2.LinkToEntityName                                =   CrmStr.salesliterature;
                le2.LinkToAttributeName                             =   CrmStr.salesliteratureid;

                
                InternalFilterExpression fi                         =   new InternalFilterExpression();
                // condition for productid
                InternalConditionExpression ex                      =   new InternalConditionExpression();
                ex.AttributeName                                    =   CrmStr.salesliteratureid;
                ex.Operator                                         =   InternalConditionOperator.Equal;
                ex.Values                                           =   new string[]{salesliteratureid.ToString()};

                //add condition to linkentity
                le2.LinkCriteria                                    =   fi;
                fi.Conditions                                       =   new InternalConditionExpression[]{ex};

                // chain linkentity2 to linkentity
                le.LinkEntities                                     =   new InternalLinkEntity[]{le2};

                //add linkentities to Query Expression 
                qe.LinkEntities                                     =   new InternalLinkEntity[]{le};
                qe.ColumnSet                                        =   RequestCols;
                
                
                InternalBusinessEntityCollection allEnt             =   (isAdmin) ? allEntQuery.adminRetrieve(qe):allEntQuery.Retrieve(qe);
                PropertyDescriptorCollection _columnDescriptors     =   null;
                EntityConfig ListMemberTConfig2                     =   new EntityConfig(wrapper,CrmStr.product);
                // Transform them in DynEntities and insert them in dynEntList
                if ( allEntQuery.RetrievedCount > 0 )
                {
                    // Transform them in DynEntities and insert them in dynEntList
                    wrapper.Util.ExecForEachDynamicEntity(allEnt,
                        delegate(InternalDynamicEntity de)
                        {
                            ADNDynProduct ob                        =   new ADNDynProduct(wrapper,de,_columnDescriptors,null);
                            ob.EntityMData                          =   ListMemberTConfig2.entityMetaData;
                            _columnDescriptors                      =   ob.PropertyDescriptorCollection;
                            List.Add(ob);
                            if ( CRMDynTraceSwitch.Sw.TraceVerbose )
                                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynProduct GetRelatedProducts found entity: {0}", ob.name),CRMDynTraceSwitch.Sw.Info);
                            return true; // continue
                        });
                }
            }
            catch (Exception ex)
            {
                string msg          =   string.Format("[E]ADNDynProduct GetRelatedProducts ex : {0}", ex);
                if (CRMDynTraceSwitch.Sw.TraceError)
                    Trace.WriteLine( msg,CRMDynTraceSwitch.Sw.Info);
                throw;
            }

            if ( CRMDynTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ADNDynProduct GetRelatedProducts END found: {0}", List.Count),CRMDynTraceSwitch.Sw.Info);
            return List;
        }

    }
}
